package org.example.design.template.template;

import org.apache.poi.ss.usermodel.IndexedColors;
import org.example.design.template.utils.POIExcelExportHSFImp;
import org.example.util.DataUtil;
import org.example.util.MethodUtils;
import org.example.util.ScanExcelBeanTool;

import java.util.List;

/**
 * @author huangshen
 * @version 0
 * @description SpecifiesColumnNameExcelImp
 */
public class SpecifiesColumnNameExcelImp extends BaseExcelExportTemplate {

    private final List<String> nameList;

    public SpecifiesColumnNameExcelImp(List<String> nameList) {
        this.nameList = nameList;
    }

    @Override
    public BaseExcelExportTemplate prepared() {
        // 获取参数名集合
        this.columnNameList = new ScanExcelBeanTool(exportBean).registerExcelColumnNameByNameList(nameList);
        // 获得导出bean的get方法集合
        this.getMethodList = new MethodUtils(exportBean).getGetMethodListByFiledNameList(nameList);
        // 初始化导出工具
        this.exportUtil = new POIExcelExportHSFImp(excelTitle, getMethodList.size());

        return this;
    }

    @Override
    public void setData(){
        for (Object targetBean : beanList) {
            if(DataUtil.isRemainderZero(totalRowNum, maxRowNum)){
                // 设置sheet名
                StringBuilder sheetName = new StringBuilder(excelTitle).append(totalRowNum / maxRowNum + 1);
                // 创建新Sheet
                exportUtil.createSheet(sheetName.toString());
            }
            for(int index = 0 ; index < getMethodList.size() ; index ++){
                // 获取该单元格数据
                String value = (String) MethodUtils.getValue(getMethodList.get(index), targetBean);
                // 设置单元格参数
                exportUtil.setRowData(value, index);
            }
            totalRowNum ++;
        }
    }
}
